package crp.core.timetable.domain.models;

import com.qwlabs.auditing.AuditedEntity;
import com.vladmihalcea.hibernate.type.array.ListArrayType;
import crp.core.course.domain.models.CourseEntity;
import crp.core.shared.Tables;
import crp.core.space.domain.models.SpaceEntity;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import java.time.DayOfWeek;
import java.time.Instant;
import java.util.List;
import java.util.Optional;

@Getter
@Setter
@Entity@TypeDef(name = "list-array", typeClass = ListArrayType.class)

@Table(name = Tables.TIMETABLE)
public class TimetableEntity extends AuditedEntity {
    @Id
    @GeneratedValue(generator = "UUID")
    @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
    private String id;
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private CourseEntity course;
    @Type(type = "list-array")
    @Column(
            name = "faculty_ids",
            columnDefinition = "text[]"
    )
    private List<String> facultyIds;
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private SpaceEntity space;
    private Integer week;
    @Enumerated(EnumType.STRING)
    private DayOfWeek dayOfWeek;
    private Instant startAt;
    private Instant endAt;
    private Integer startSlot;
    private Integer endSlot;

    public String getCourseId() {
        return course.getId();
    }

    public String getSpaceId() {
        return Optional.ofNullable(space)
                .map(SpaceEntity::getId)
                .orElse(null);
    }
}
